﻿Public Class FormProveedoresListado

    Dim classController As New Class_RC_Controller.rc_Proveedores()

    Private m_frmProveedor As FrmProveedores
    Public Property frmProveedores() As FrmProveedores
        Get
            Return m_frmProveedor
        End Get
        Set(value As FrmProveedores)
            m_frmProveedor = value
        End Set
    End Property

    Private Sub FormProveedoresListado_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        dgProveedor.AutoGenerateColumns = False
        CargarCombos()
        CargarProveedores()
    End Sub

    Private Sub CargarProveedores()

        Dim codCiudad As Integer
        If cbxCiudad.Checked Then
            codCiudad = cboCiudad.SelectedValue
        End If

        Dim dt As New DataTable
        dt = classController.get_Proveedores_All(oiml.dbConn, 0, txtNombre.Text, txtRuc.Text, codCiudad)
        dgProveedor.DataSource = dt

    End Sub

    Private Sub CargarCombos()

        Dim dsCiudad As New DataTable
        dsCiudad = classController.get_Ciudades(oiml.dbConn)
        If Not dsCiudad Is Nothing Then
            If dsCiudad.Rows.Count > 0 Then
                cboCiudad.DataSource = dsCiudad
                cboCiudad.DisplayMember = "descripcion"
                cboCiudad.ValueMember = "codCiudad"
            End If
        End If
    End Sub

    Private Sub btnCancelar_Click(sender As System.Object, e As System.EventArgs) Handles btnCancelar.Click
        Me.Close()

    End Sub

    Private Sub btnEditar_Click(sender As System.Object, e As System.EventArgs) Handles btnEditar.Click
        Try
            If dgProveedor.SelectedRows.Count > 0 Then
                Dim cod As New Integer
                cod = dgProveedor.SelectedRows(0).Cells(0).Value

                If frmProveedores Is Nothing Then

                    frmProveedores = New FrmProveedores
                    frmProveedores.codProveedor = cod

                    'If frmClientes.ShowDialog() = Windows.Forms.DialogResult.OK Then
                    frmProveedores.ShowDialog()
                    CargarProveedores()
                    frmProveedores.Dispose()
                    frmProveedores = Nothing
                    'End If
                Else
                    frmProveedores.codProveedor = cod
                    frmProveedores.ShowDialog()
                End If

            End If

        Catch ex As Exception
            MessageBox.Show("Error: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

        End Try

    End Sub

    Private Sub txtBuscar_Click(sender As System.Object, e As System.EventArgs) Handles txtBuscar.Click
        CargarProveedores()
    End Sub

    Private Sub txtNombre_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles txtNombre.KeyDown, txtRuc.KeyDown
        If e.KeyCode = Keys.Enter Then
            CargarProveedores()

        End If
    End Sub

    Private Sub dgProveedor_DoubleClick(sender As System.Object, e As System.EventArgs) Handles dgProveedor.DoubleClick
        btnEditar_Click(sender, e)
    End Sub

    Private Sub dgProveedor_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles dgProveedor.KeyDown
        If e.KeyCode = Keys.Enter Then
            btnEditar_Click(sender, e)
        End If

    End Sub

    Private Sub cbxCiudad_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles cbxCiudad.KeyDown
        If e.KeyCode = Keys.Enter Then
            CargarProveedores()
        End If
    End Sub

    Private Sub cboCiudad_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles cboCiudad.KeyDown
        If e.KeyCode = Keys.Enter Then
            CargarProveedores()
        End If
    End Sub

End Class